package com.cn.zmc.design.chian.factory;

import com.cn.zmc.design.chian.service.ChainService;
import com.cn.zmc.design.chian.service.impl.ChainServiceA;
import com.cn.zmc.design.chian.service.impl.ChainServiceB;
import com.cn.zmc.design.chian.service.impl.ChainServiceC;
import com.cn.zmc.design.chian.service.impl.ChainServiceD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ChainFactory {


    @Autowired
    private ChainServiceA chainServiceA;
    @Autowired
    private ChainServiceB chainServiceB;
    @Autowired
    private ChainServiceC chainServiceC;
    @Autowired
    private ChainServiceD chainServiceD;

    /*
    *  逻辑A责任链
    * */
    public ChainService getFactory1() {
        chainServiceA.setChainService(chainServiceB);
        chainServiceB.setChainService(chainServiceC);
        chainServiceC.setChainService(chainServiceD);
        chainServiceD.setChainService(null);
        return chainServiceA;
    }

    /*
     *  逻辑B责任链
     * */
    public ChainService getFactory2() {
        chainServiceB.setChainService(chainServiceA);
        chainServiceA.setChainService(chainServiceC);
        chainServiceC.setChainService(chainServiceD);
        chainServiceD.setChainService(null);
        return chainServiceB;
    }

    /*
     *  逻辑C责任链
     * */
    public ChainService getFactory3() {
        chainServiceC.setChainService(chainServiceB);
        chainServiceB.setChainService(chainServiceA);
        chainServiceA.setChainService(chainServiceD);
        chainServiceD.setChainService(null);
        return chainServiceC;
    }

    /*
     *  逻辑D责任链
     * */
    public ChainService getFactory4() {
        chainServiceD.setChainService(chainServiceB);
        chainServiceB.setChainService(chainServiceC);
        chainServiceC.setChainService(chainServiceA);
        chainServiceA.setChainService(null);
        return chainServiceD;
    }


    public ChainService getFactoryByType(long id) {
        if (id == 1) {
            return getFactory1();
        } else if (id == 2) {
            return getFactory2();
        } else if (id == 3) {
            return getFactory3();
        } else {
            return getFactory4();
        }
    }
}
